package com.lute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ide.eclipse.quickfix.jdt.processors.RequestMappingDialog.Method;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.Approver;
import com.lute.model.BusinessUnit;
import com.lute.model.Client;
import com.lute.model.ClientAccountingPeriodState;
import com.lute.model.ClientAdmin;
import com.lute.model.ClientProfile;
import com.lute.model.Employee;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.User;
import com.lute.model.UserEntitlement;
import com.lute.utils.ServerErrorResponse;

@Controller
public class ClientAdminGetBusinessUnitsServlet {
	
	@RequestMapping (value="/getBusinessUnits",method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String getBusinessUnits(@RequestBody String dataJSON, HttpServletRequest request) throws JSONException {
		String result = "";
		String clientId;
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(dataJSON);
			clientId = jsonReq.getString("clientId");
		} catch(JSONException e) {
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		/* check if json data was not malformed */
		if((jsonReq.isNull("clientId"))) {
			jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String role = (String)session.getAttribute("role");
		
		/* check  authorization */
		if(!((role.equals("clientAdmin")))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		
		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		int id_client = Integer.parseInt(clientId);
		
		/* check if clientAdmin has privilege to client */
		ClientProfile clientProfile = UserEntitlement.checkClientAdminPrivilegeToClientProfile(sessionUserId, id_client);
		if(clientProfile == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		List<JSONObject> businessUnits = new ArrayList<JSONObject>();
		
		try {
			
			List<BusinessUnit> units = new ArrayList<BusinessUnit>();
			Client client = Client.getClientWithBusinessUnitsByIdFromDB("id_client", id_client);
			units = client.getBusiness_units();
			
			for(BusinessUnit unit: units) {
				JSONObject obj = new JSONObject();
				obj.put("businessUnitId", unit.getId_business_unit());
				obj.put("businessUnitName",  unit.getName());
				businessUnits.add(obj);
			}
		}catch(NullPointerException npe){
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
		
		jsonRes.put("result","OK");
		jsonRes.put("businessUnits",businessUnits);
		result = jsonRes.toString();
		return result;
	}
}